<?php
// 酒店公寓管理
class HotelAction extends CommonAction {
    function _filter(&$map) {
         $key = trim($_REQUEST['keywords']);
         if(!empty($key)) {
             $map['name'] = array('like',"%".$key."%");
             $map['address'] = array('like',"%".$key."%");
             $map['_logic'] = "or";
         }
    }

    function _list($model, $map, $sortBy = '', $asc = false){
        parent::_list($model, $map, $sortBy = 'id', $asc = true);
    }

    function _before_add(){
        $this->assign('shdistrict',C('district'));
    }

     function _before_edit(){
        $this->assign('shdistrict',C('district'));
        $Attach = M('Attach');
        $logo = $Attach ->where('hotel_id ='.$_REQUEST['id'].' and attachfunc = 2')->field('id,filename,path')->find();
		if(!empty($logo['filename'])){
			$this -> assign('logo_photo',__ROOT__.substr($logo['path'],1).'thumb_'.$logo['filename']);
		}
        $attach_photo = $Attach ->where('hotel_id ='.$_REQUEST['id'].' and attachfunc = 1')->order('sort')->findAll();
        $this -> assign('attach_photo',$attach_photo);
    }

    /**
     *指定不同的人负责某几个公寓
     * @param <type> $id 房型ID
     */
    public function hotelCharge(){
        $id = intval($_REQUEST['id']);//只取一个公寓
        $this->assign('hotel',M('Hotel')->where('id ='.$id )->field('name')->find() );
        $this -> assign('hotelId',$id);
        $this -> assign('users',M('User')->where('status =1')->findAll()  );
        if(!empty($_POST['user_id']) ) {
            $userArray = $_REQUEST['user_id'];
            $hotelUser = M('Hotel_user');
            $hotelUser -> where('hotel_id ='.$id)->delete();//delete all the hotel
            foreach ($userArray as $v) {
                $data['hotel_id'] = $id;
                $data['user_id'] = $v;
                $hotelUser ->add($data);
            }
            $this->success('分店分配成功！');
        }
        $this->display('charge');
    }

    public function insert() {
        $Hotel = D('Hotel');
        if(false == $Hotel->create()) {
            $this->error($Hotel->getError());
        }
        if(false !== $list = $Hotel->add()) {
            $this->process_photo($list,0);
            $this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
            $this->success('公寓添加成功');
        }
        else {
            $this->error('新增公寓失败！');
        }
    }

    public function update(){
        $id = intval($_POST['id']);
        if($id <= 0){
            $this->error('编辑的产品不合法！');
        }
        $Hotel = D('Hotel');
        if(false == $Hotel->create()) {
            $this->error($Hotel->getError());
        }
        if(false !== $list = $Hotel->save()) {
            $this->process_photo($id,0);
            $this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
            $this->success('公寓修改成功');
        }
        else {
            $this->error('公寓修改失败！');
        }
    }

}
?>
